<template>
	<view class="contain">
		<!-- 空白页/无网络-->
		<empty tips="暂无收货地址~" src="empty/await" v-if="empty">
			<view class="btn-link" @click="addAddress('add')">新增地址</view>
		</empty>
		<block v-else>
			<view class="list" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
				<view class="wrapper">
					<view class="list-t">
						<text class="text">{{item.addressee}}</text>
						<text class="text mobile">{{item.addresseePhone}}</text>
						<text v-if="item.isDefault == 1" class="tag">默认</text>
					</view>
					<view class="address">
						<text class="text">{{item.region.join(',')}} {{item.address.replace(/\|/g,' ')}}</text>
					</view>
				</view>
				<text class="iconfont icon-bianji" @click.stop="addAddress('edit', item)"></text>
			</view>
			<view class="fixed-bottom">
				<view class="b-card">
					<button class="add-btn no-border" @click="addAddress('add')">新增地址</button>
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoad:false,
				source: 0,
				addressList: [],
				params:{
					pageNum: 1,
					pageSize: 100
				},
			}
		},
		onLoad(option){
			this.listConsignee()
			this.source = option.source;
		},
		computed:{
			empty() {
				return this.addressList.length==0&&this.isLoad
			}
		},
		methods: {
			async listConsignee() {
				const {code, data} = await this.$apis.postApi('/address/list',this.params)
				this.isLoad = true
				if(code == 200) {
					this.addressList = data.list.map(item => {
						const region = [item.provinceCode,item.cityCode,item.districtCode]
						item.region = region
						return item
					})
				}
			},
			//选择地址
			checkAddress(item){
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
					item.address = item.address.replace('|',' ')
					this.$api.prePage().addressData = item;
					uni.navigateBack()
				}
			},
			addAddress(type, item){
				uni.navigateTo({
					url: `/userPages/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				if(type=='delete') {
					this.addressList.forEach((item,index) => {
						if(item.addressId==data.addressId){
							this.addressList.splice(index,1)
						}
					})
				} else {
					this.listConsignee()
				}
			}
		}
	}
</script>

<style lang='scss' scoped>
	.tips{
		font-size: $font-sm + 2upx;
		color: $font-color-light;
		padding: 20upx 0;
		text-align: center;
		width: 100%;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 25upx 24upx;;
		background: #fff;
		position: relative;
		min-height: 140upx;
		margin: 20upx;
		border-radius: 16upx;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
		font-size: $font-sm;
		.list-t{
			display: flex;
			align-items: center;
			.text{
				font-size: 30upx;
				font-weight: bold;
			}
			.mobile{
				font-size: 32upx;
				padding-left: 20upx;
				font-weight: normal;
			}
		}
		.address{
			font-weight: 500;
			font-size: 26upx;
			padding-top: 20upx;
			text{
				color: #666;
			}
		}	
		.tag{
			font-size: 22upx;
			font-weight: bold;
			width: 58upx;
			height: 36upx;
			background: #4083F0;
			border-radius: 4upx;
			margin-left: 20upx;
			display: flex;
			justify-content: center;
			align-content: center;
			color: #fff;
		}
	}
	
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}
	
</style>
